<?php

namespace App\Http\Controllers\Admin;

use App\Http\Repository\AdminRepository;
use App\Http\Repository\ChunkRepository;
use App\Http\Repository\InfoRepository;
use App\Http\Repository\SortRepository;
use App\Http\Repository\UserRepository;
use App\Http\Servers\Admin\FileServer;
use App\Http\Servers\Admin\InfoServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class InfoController extends CommonController
{
    //信息分类列表
    public function info(Request $request, InfoRepository $infoRepository, AdminRepository $adminRepository, UserRepository $userRepository, SortRepository $sortRepository, InfoServer $infoServer)
    {
        $data['chunk'] = \App\Chunk::findorfail($request->chunk);

        if ($message = $infoServer->infoDeleteAndChangeSort($this, $request, $data['chunk'])) {
            return $message;
        }

        if ($message = $infoServer->infoDelete($request, $data['chunk'])) {
            return $message;
        }

        if ($message = $infoServer->infoStatusChange($request, $data['chunk'])) {
            return $message;
        }


        $data['infos'] = $infoRepository->getInfo($request->chunk, $request);

        $data['admins'] = $adminRepository->getAdminsByLists();

        $data['users'] = $userRepository->getUsersByLists();

        $data['sorts'] = $sortRepository->getSortsByX($request->chunk);

        $data['append']['sort'] = $request->sort;
        $data['append']['is'] = $request->is;
        $data['append']['search'] = $request->search;
        $data['append']['type'] = $request->type;

//        dd($data);

        return view('admin.info.info', $data);
    }

    //信息分类列表
    public function trash(Request $request, InfoRepository $infoRepository, AdminRepository $adminRepository, UserRepository $userRepository, SortRepository $sortRepository, InfoServer $infoServer)
    {
        $data['chunk'] = \App\Chunk::findorfail($request->chunk);

        if ($message = $infoServer->infoRestoreAndForce($this, $request, $data['chunk'])) {
            return $message;
        }

        if ($message = $infoServer->infoRestore($request, $data['chunk'])) {
            return $message;
        }

        if ($message = $infoServer->infoForce($request, $data['chunk'])) {
            return $message;
        }

        $data['infos'] = $infoRepository->getInfoOnlyTrashed($request->chunk, $request);

        $data['admins'] = $adminRepository->getAdminsByLists();

        $data['users'] = $userRepository->getUsersByLists();

        $data['sorts'] = $sortRepository->getSortsByX($request->chunk);

        $data['append']['sort'] = $request->sort;
        $data['append']['is'] = $request->is;
        $data['append']['search'] = $request->search;
        $data['append']['type'] = $request->type;

        return view('admin.info.trash', $data);
    }

    //模块信息添加
    public function info_add(Request $request, InfoServer $infoServer, SortRepository $sortRepository, ChunkRepository $chunkRepository, FileServer $fileServer)
    {
        if ($request->isMethod('post')) {
            $chunk = $chunkRepository->getChunkWithExtends($request->chunk);
            $validate = [
                'name' => 'required',
                'xindex' => 'required',
                'theme' => 'required|info_theme',
                'page_view' => 'required',
                'tag' => 'required',
                'sort' => 'required',
            ];
//            foreach ($chunk['extends'] as $field) {
//                $validate[$field] = 'required';
//            }
            $this->validate($request, $validate);

            $create['name'] = $request->name;
            $create['xindex'] = $request->xindex;
            $create['theme'] = $request->theme;
            $create['page_view'] = $request->page_view;
            $create['tag'] = $request->tag;
            $create['author'] = \Auth::guard('admin')->user()->id;
            $create['author_role'] = 1;
            $create['describe'] = $request->describe;
            $create['simpledescribe'] = $request->has('simpledescribe') ?
                $request->simpledescribe
                : subtext(strip_tags($request->describe), config('app.simple_len'));
            $create['status'] = $request->has('status') ? 1 : 0;
            $create['is_hot'] = $request->has('is_hot') ? 1 : 0;
            $create['is_push'] = $request->has('is_push') ? 1 : 0;
            $create['is_new'] = $request->has('is_new') ? 1 : 0;

            $create['extend_1'] = $request->extend_1;
            $create['extend_2'] = $request->extend_2;
            $create['extend_3'] = $request->extend_3;
            $create['extend_4'] = $request->extend_4;
            $create['extend_5'] = $request->extend_5;
            $create['extend_6'] = $request->extend_6;
            $create['extend_7'] = $request->extend_7;
            $create['extend_8'] = $request->extend_8;
            $create['extend_9'] = $request->extend_9;

            foreach ($chunk['extends'] as $field) {
                $create[$field] = $request->$field;
            }

            if ($request->hasFile('file')) {
                $create['file'] = $fileServer->uploadFile($request, $chunk);
            }

            $result = \App\Info::create($create);
            if ($result) {
                foreach (explode(',', rtrim($request->sort, ',')) as $sort) {
                    \App\InfoSortLines::create(['info' => $result->id, 'sort' => $sort, 'chunk' => $chunk->id]);
                }
                if ($request->has('images')) {
                    foreach ($request->images as $image) {
                        \App\InfoImageLines::create(['info' => $result->id, 'image' => $image]);
                    }
                }
                $message['message'] = message(true, '添加成功', $chunk->name . '信息 ' . $request->name . ' 添加成功!');
            } else {
                $message['message'] = message(false, '添加失败', $chunk->name . '信息 ' . $request->name . ' 添加失败!');
            }
            $message['selectSort'] = explode(',', rtrim($request->sort, ','));
            return back()->with($message);
        }

        $data['chunk'] = $chunkRepository->getChunkWithExtends($request->chunk);

        $data['sorts'] = $sortRepository->getSortsByX($request->chunk);
        if (empty($data['sorts'])) {
            $message['message'] = message(false, '无法添加', $data['chunk']->name . ' 模块中无信息分类,无法添加信息,请先添加分类!');
            return redirect()->route('admin.sort.sort_add', $data['chunk']->id)->with($message);
        }

        $data['themes'] = $infoServer->getInfoTheme();

        $data['sort'] = $request->sort;

        $tmpTag = explode(',', config('set.web_keywords'));
        shuffle($tmpTag);
        $data['tag'] = $tmpTag[0];
        return view('admin.info.info_add', $data);
    }

    //模块信息编辑
    public function info_edit(Request $request, InfoServer $infoServer, SortRepository $sortRepository, ChunkRepository $chunkRepository, InfoRepository $infoRepository, FileServer $fileServer)
    {
        if ($request->isMethod('post')) {
            $chunk = $chunkRepository->getChunkWithExtends($request->chunk);
            $info = $infoRepository->getInfoWithImageAndSort($request->id);

            $validate = [
                'id' => 'required',
                'name' => 'required',
                'xindex' => 'required',
                'theme' => 'required|info_theme',
                'page_view' => 'required',
                'tag' => 'required',
                'sort' => 'required',
            ];
//            foreach ($chunk['extends'] as $field) {
//                $validate[$field] = 'required';
//            }
            $this->validate($request, $validate);

            $update['name'] = $request->name;
            $update['xindex'] = $request->xindex;
            $update['theme'] = $request->theme;
            $update['page_view'] = $request->page_view;
            $update['tag'] = $request->tag;
            $update['author'] = \Auth::guard('admin')->user()->id;
            $update['author_role'] = 1;
            $update['describe'] = $request->describe;
            $update['simpledescribe'] = $request->has('simpledescribe') ?
                $request->simpledescribe
                : subtext(strip_tags($request->describe), config('app.simple_len'));
            $update['status'] = $request->has('status') ? 1 : 0;
            $update['is_hot'] = $request->has('is_hot') ? 1 : 0;
            $update['is_push'] = $request->has('is_push') ? 1 : 0;
            $update['is_new'] = $request->has('is_new') ? 1 : 0;

            $update['extend_1'] = $request->extend_1;
            $update['extend_2'] = $request->extend_2;
            $update['extend_3'] = $request->extend_3;
            $update['extend_4'] = $request->extend_4;
            $update['extend_5'] = $request->extend_5;
            $update['extend_6'] = $request->extend_6;
            $update['extend_7'] = $request->extend_7;
            $update['extend_8'] = $request->extend_8;
            $update['extend_9'] = $request->extend_9;

            if (!empty($chunk['extends'])) {
                foreach ($chunk['extends'] as $field) {
                    $update[$field] = $request->$field;
                }
            }

            if ($request->hasFile('file')) {
                $update['file'] = $fileServer->uploadFile($request, $chunk);
            }

            $result = \App\Info::where('id', $request->id)->update($update);
            if ($result) {
                \App\InfoSortLines::where('info', $request->id)->delete();
                foreach (explode(',', rtrim($request->sort, ',')) as $sort) {
                    \App\InfoSortLines::create(['info' => $request->id, 'sort' => $sort, 'chunk' => $chunk->id]);
                }
                \App\InfoImageLines::where('info', $request->id)->delete();
                if ($request->has('images')) {
                    foreach ($request->images as $image) {
                        \App\InfoImageLines::create(['info' => $request->id, 'image' => $image]);
                    }
                }
                if (!empty($info->file)) {
                    unlink('.' . $info->file);
                }
                $message['message'] = message(true, '编辑成功', $chunk->name . '信息 ' . $request->name . ' 编辑成功!');
            } else {
                $message['message'] = message(false, '编辑失败', $chunk->name . '信息 ' . $request->name . ' 编辑失败!');
            }
            return back()->with($message);
        }

        $data['info'] = $infoRepository->getInfoWithImageAndSort($request->id);
        $i = 0;
        foreach ($data['info']['images'] as $image) {
            $i == 0 && $data['info']['cover'] = $image;
            $i++;
        }
        $data['chunk'] = $chunkRepository->getChunkWithExtends($request->chunk);

        $data['themes'] = $infoServer->getInfoTheme();

        $data['sorts'] = $sortRepository->getSortsByX($request->chunk);

        return view('admin.info.info_edit', $data);
    }
}
